// pages/address/address.js
var app = getApp();var pic = [];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_id:0,
    nickname: "",
    username: "",
    phone: "",
    bank_name: "",
    bank_number: "",
    isDisabled: false,
    isComplete: false,
    buttonTitle: "保存",

    region: [],
    area:'',

    province_list:[],
    province:'请选择',
    sheng:'',
    city_list:[],
    city:'请选择',
    shi:'',
    district_list:[],
    district:'请选择',
    qu:'',


    address:'',

    town:[],
    towns:[],
    xiang:'请选择',
    xiang_id:'',
    jiedao:'',
    jiedao_id:'',



    nickname:'',
    images:[],
    tixian:0
  },
  formSubmit(e) {
    const addrInfo = e.detail.value;
    var that = this;
    
    //var l = this.data.region.toString();
    var l = this.data.province+','+this.data.city+','+this.data.district;
    var jiedao = this.data.xiang?this.data.xiang:this.data.jiedao;
    var jiedao_id = this.data.xiang_id?this.data.xiang_id:this.data.jiedao_id;

    if(addrInfo.nickname==""||addrInfo.username==""||addrInfo.phone==""||l==""){
      this.data.isComplete=false;
      wx.showModal({
        title:"提示",
        content:"请完善信息",
        showCancel:false
      }); 
      return false;
    }else if(!/^[1][3,4,5,7,8]\d{9}$/.test(addrInfo.phone)){
      wx.showModal({
        title:"提示",
        content:"手机号格式不规范",
        showCancel:false
      }); 
      return false;
    }else{
      this.data.isComplete=true;
      console.log(addrInfo);
      
      var images = this.data.images;
      pic = [];
      that.setData({
        upload_pic:[]
      })
      for(var i=0;i<=images.length;i++){
          that.upload(images[i],1)
      }  
    
      wx.showModal({
        title: '提示',
        content: '请确认您填的信息是否准确',
        success: function (sm) {
          if (sm.confirm) {
            wx.showLoading({
              title: '请稍后',
            })
            // 显示合作伙伴
            wx.request({
              url: app.globalData.httpUrl+"api/mini/userinfo",
              header: {
                  'content-type': 'application/x-www-form-urlencoded','appid':app.globalData.appId
              },
              method: 'POST',
              dataType: 'json',
              data:{id:that.data.user_id,username:addrInfo.username,phone:addrInfo.phone,'platform':'member',headimg:JSON.stringify(that.data.upload_pic)},
              success: function (res) {
                wx.showToast({
                  title: '保存成功',
                })
                that.user();
                that.user_info(that.data.user_id);
                that.setData({
                  isDisabled: false,
                  isComplete: true,
                })
                setTimeout(function(){
                  wx.navigateBack({});
                  wx.hideLoading() ;
                },2500)
              }
            })
          }
        }
      })
    }
  },

  //更改按钮
  updateAddr(e){
    this.setData({
      isDisabled:false,
      isComplete:false,
      buttonTitle: "保存"
    });
  },
  //查看多图
  previewImage: function(e) {
    if(e.currentTarget.dataset.id==2){
      wx.previewImage({
        urls: this.data.images2
      });
    }else{
      wx.previewImage({
        urls: this.data.images
      });
    }     
  },
   //上传多图
   chooseImage1: function(e) {
    var o = this;
    
    //var i = this.data.images;
    
    wx.chooseImage({
        //count: 1 - i.length - o.data.images.length,
        count: 1,
        sizeType: [ "compressed" ],
        sourceType: [ "album", "camera" ],
        success: function(e) {
            pic = '';
            o.setData({
              images:[]
            })
            var i = o.data.images;
            var t = e.tempFilePaths;
            i = i.concat(t),  o.setData({
                images: i,
            });
        }
    });
    
  },

  //删除图片
  delete1: function(e) {
      var a = e.currentTarget.dataset.index, t = this.data.images;
      t.splice(a, 1), this.setData({
          images: t
      }); 
  },

  //上传图片
  upload:function(path,flag){
    var that = this;
    wx.uploadFile({
        url: app.globalData.httpUrl+"api/mini/uploads",
        filePath: path,
        name: "upfile",
        formData: null,
        success: function(e) {
            console.log(e.data);
    
            pic = pic.concat(e.data), that.setData({
              upload_pic: pic
            });           
            
        }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '请稍后',
    })
    this.setData({
      user_id:app.globalData.userInfo?app.globalData.userInfo.id:2
    })
    if(options.user_id){
      this.setData({
        user_id:options.user_id
      })
    }
    if(options.tixian){
      this.setData({
        tixian:options.tixian
      })
    }
    console.log(this.data.user_id)
    this.user();
    this.province_list();
  },

  //省
  province_list(){
    var that = this
    // 显示合作伙伴
    wx.request({
      url: app.globalData.httpUrl+"api/mini/province_list",
      header: {
          'content-type': 'application/x-www-form-urlencoded','appid':app.globalData.appId
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        that.setData({
          province_list:res.data
        })
      }
    })
  },
  

  //绑定省
  bindProvinceChange(e){
    this.setData({
      province:this.data.province_list[e.detail.value]
    })
    this.city_list();
  },

   //市
   city_list(){
    var that = this
    // 显示合作伙伴
    wx.request({
      url: app.globalData.httpUrl+"api/mini/city_list",
      header: {
          'content-type': 'application/x-www-form-urlencoded','appid':app.globalData.appId
      },
      method: 'POST',
      dataType: 'json',
      data:{province:that.data.province},
      success: function (res) {
        that.setData({
          city_list:res.data
        })
      }
    })
  },

  //绑定市
  bindCityChange(e){
    this.setData({
      city:this.data.city_list[e.detail.value]
    })
    this.district_list();
  },

   //区
   district_list(){
    var that = this
    // 显示合作伙伴
    wx.request({
      url: app.globalData.httpUrl+"api/mini/district_list",
      header: {
          'content-type': 'application/x-www-form-urlencoded','appid':app.globalData.appId
      },
      method: 'POST',
      dataType: 'json',
      data:{city:that.data.city},
      success: function (res) {
        that.setData({
          district_list:res.data
        })
      }
    })
  },

   //绑定区
   bindDistrictChange(e){
    this.setData({
      district:this.data.district_list[e.detail.value]
    })
    var that = this;
    wx.request({
      url: app.globalData.apiurl+"api/mini/regions",
      cachetime: "0",
      header: {
          'content-type': 'application/x-www-form-urlencoded','appid':app.globalData.appId
      },
      method: 'POST',
      dataType: 'json',
      data: {
        district: that.data.district
      },
      success: function(e) { 
        console.log(e);
          that.setData({
              town:e.data.id,
              towns:e.data.name
          })
          if(that.data.towns){
              that.setData({
                xiang:that.data.towns[0],
                xiang_id:that.data.town[0]
              })
          }else{
            that.setData({
              xiang:'',
              xiang_id:0
            })
          }
      }
    })
  },

  fanhui(){
    wx.navigateBack(-1)
  },

  // 个人中心
  user() {
    var that = this
    // 显示合作伙伴
    wx.request({
      url: app.globalData.httpUrl+"api/mini/user",
      header: {
          'content-type': 'application/x-www-form-urlencoded','appid':app.globalData.appId
      },
      method: 'POST',
      dataType: 'json',
      data:{id:that.data.user_id},
      success: function (res) {
        that.setData({
          user_info:res.data
        })

        var user_info = res.data
        var area = user_info.area
        if(area){
          var area_list = area.split(' ');
          that.setData({
            nickname:user_info.nickname,
            images:[user_info.headimg],
            username:user_info.username,
            phone:user_info.phone,
            bank_name:user_info.bank_name,
            bank_number:user_info.bank_number,
  
            address:user_info.address,
  
            region: user_info.region,
            area: user_info.area,
  
            sheng:area_list[0],
            shi:area_list[1],
            qu:area_list[2],
  
            jiedao:user_info.town,
            jiedao_id:user_info.town_id,
            
            isDisabled: true,
            isComplete:true,
            buttonTitle: "保存"
          });
        }else{
          that.setData({
            nickname:user_info.nickname,
            images:[user_info.headimg],
            username:user_info.username,
            phone:user_info.phone,
            bank_name:user_info.bank_name,
            bank_number:user_info.bank_number,
  
            address:user_info.address,
  
            region: user_info.region,
            area: user_info.area,
  
  
            jiedao:user_info.town,
            jiedao_id:user_info.town_id,
            
            isDisabled: true,
            isComplete:true,
            buttonTitle: "保存"
          });
        }
        
        that.huixian_town(user_info.region,user_info.town);
        wx.hideLoading({
          success: (res) => {},
        })
        that.user_info(that.data.user_id);
      }
    })
  },

  //绑定手机
  getPhoneNumber: function (e) {
    console.log(e);
    var that = this
    const code = e.detail.code
    wx.request({
      url: app.globalData.httpUrl+"api/mini/get_phone",
      header: {
          'content-type': 'application/x-www-form-urlencoded','appid':app.globalData.appId
      },
      method: 'POST',
      dataType: 'json',
      data:{code:code},
      success: function (res) {
        that.setData({
          phone:res.data
        })
        
      }
    })
  },


  //用户
  user_info:function(id){
    var that = this;
    wx.request({
      url: app.globalData.httpUrl+"api/mini/user_info",
      header: {
          'content-type': 'application/x-www-form-urlencoded','appid':app.globalData.appId
      },
      method: 'POST',
      data:{'id':id},
      dataType: 'json',
      success: function (res) {
          app.globalData.userInfo = res.data;      
      }
    })
  },

    //回显街道
    huixian_town(region,jiedao){
      var that = this;
      wx.request({
        url: app.globalData.apiurl+"api/mini/region",
        cachetime: "0",
        header: {
            'content-type': 'application/x-www-form-urlencoded','appid':app.globalData.appId
        },
        method: 'POST',
        dataType: 'json',
        data: {
            region: region
        },
        success: function(e) { 
            that.setData({
                town:e.data.id,
                towns:e.data.name
            })
            if(jiedao==null){
                if(that.data.towns){
                    that.setData({
                        jiedao:that.data.towns[0],
                        jiedao_id:that.data.town[0]
                    })
                }
            }
        }
    })
  },


    //切换区域
    bindRegionChange: function(e) {
        console.log("picker发送选择改变，携带值为", e.detail.value), this.setData({
            region: e.detail.value
        });
        var that = this;
        wx.request({
            url: app.globalData.apiurl+"api/mini/region",
            cachetime: "0",
            header: {
                'content-type': 'application/x-www-form-urlencoded','appid':app.globalData.appId
            },
            method: 'POST',
            dataType: 'json',
            data: {
                region: e.detail.value
            },
            success: function(e) { 
              console.log(e);
                that.setData({
                    town:e.data.id,
                    towns:e.data.name
                })
                if(that.data.towns){
                    that.setData({
                      jiedao:that.data.towns[0],
                      jiedao_id:that.data.town[0]
                    })
                }else{
                  that.setData({
                    jiedao:'',
                    jiedao_id:0
                  })
                }
            }
        })
    },

    //切换乡镇
    bindTownChange(e){
        if(this.data.towns){
            console.log("picker发送选择改变，携带值为", e.detail.value), this.setData({
                xiang: this.data.towns[e.detail.value],
                xiang_id: this.data.town[e.detail.value]
            });
        }
        console.log(this.data.xiang_id);
    },

    // dingwei: function(e) {
    //     console.log(e);
    //     var a = this;
    //     wx.chooseLocation({
    //         success: function(e) {
    //             console.log(e);
    //             var t = e.address.indexOf("区");
    //             console.log(e.address.substring(t + 1) + e.name), a.setData({
    //                 detail: e.address.substring(t + 1) + e.name
    //             });
    //         },
    //         fail: function() {
    //             wx.getSetting({
    //                 success: function(e) {
    //                     console.log(e), e.authSetting["scope.address"] ? a.quxiao() : wx.showModal({
    //                         title: "提示",
    //                         content: "您拒绝了获取收货地址授权，部分功能无法使用,点击确定重新获取授权。",
    //                         showCancel: !1,
    //                         success: function(e) {
    //                             e.confirm && wx.openSetting({
    //                                 success: function(e) {
    //                                     e.authSetting["scope.address"] && a.dingwei();
    //                                 },
    //                                 fail: function(e) {}
    //                             });
    //                         }
    //                     });
    //                 }
    //             });
    //         }
    //     });
    // },

    //取消
    // quxiao(){
    //     wx.showToast({
    //         title: '暂未开通',
    //     })
    //     return false;
    // },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})